﻿
//关闭当前页 返回父页面
function closePage(isReload) {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
    if (isReload) {
        parent.search();
    }
}

layui.use(['form', 'layer', 'laydate', 'upload'], function () {
    var form = layui.form,
        layer = layui.layer,
        laydate = layui.laydate,
        upload = layui.upload;

    var uploadProjectImagesUrl = '/Business/Common/UploadifyHandler.ashx?PostType=upload&ReocrdType=ProjectImages';

    //上传图片附件
    upload.render({
        elem: '#tbUploadImageBtn'
        , url: uploadProjectImagesUrl
        , accept: 'images'
        , acceptMime: 'image/*'
        //, exts: 'jpg|png|gif'
        //, size: 50
        , before: function (obj) {
            layer.load(); //上传loading
        }
        , done: function (res, index) {
            layer.closeAll('loading');
            if (res.AnnexPath) {
                var divHtml = '<div>';
                divHtml += '<img class="fileItem" src="' + res.AnnexPath + '" data-annexpath="' + res.AnnexPath + '" data-annexname="' + res.AnnexName
                    + '" data-annexsize="' + res.AnnexSize + '" data-recordtype="' + res.RecordType
                    + '" alt="' + res.AnnexName + '" onclick="viewImage("' + tbImagesList +'")" />';
                divHtml += '<a class="file-close" onclick="closeListItem(this)">X</a>';
                divHtml += '</div>';
                $('#tbImagesList').append(divHtml);
            }
        }
        , error: function (index, upload) {
            layer.closeAll('loading');
        }
    });

    var uploadProjectFilesUrl = '/Business/Common/UploadifyHandler.ashx?PostType=upload&ReocrdType=ProjectFiles';

    //上传图片附件
    upload.render({
        elem: '#tbUploadFileBtn'
        , url: uploadProjectFilesUrl
        , accept: 'file'
        , acceptMime: 'file/*'
        //, exts: 'xls|xlsx|word|pdf|zip'
        //, size: 50
        , before: function (obj) {
            layer.load(); //上传loading
        }
        , done: function (res, index) {
            layer.closeAll('loading');
            if (res.AnnexPath) {
                var divHtml = '<div>';
                divHtml += '<a class="fileItem" target="_blank" href="' + res.AnnexPath + '" data-annexpath="' + res.AnnexPath + '" data-annexname="' + res.AnnexName
                    + '" data-annexsize="' + res.AnnexSize + '" data-recordtype="' + res.RecordType + '">' + res.AnnexName + '</a>';
                divHtml += '<a class="file-close" onclick="closeListItem(this)">X</a>';
                divHtml += '</div>';
                $('#tbFilesList').append(divHtml);
            }
        }
        , error: function (index, upload) {
            layer.closeAll('loading');
        }
    });

    layer.selectMap = function (value) {
        $("#tbCoordinate").val(value);
    }

    laydate.render({
        elem: '#tbStartDate',
        type: 'date',
        trigger: 'click'
    });

    laydate.render({
        elem: '#tbEndDate',
        type: 'date',
        trigger: 'click'
    });

    $(function () {
        if (app.getRequest('GUID')) {
            initData();
        }
    });

    //初始化表单
    function initData() {
        var fieldData = {
            GUID: app.getRequest('GUID'),
            PostType: "get",
            ActionType: "Project"
        };
        app.loadData("Project.ashx", fieldData, function (data) {
            $('#tbStartDate').val(app.getTDate2(data.StartDate));
            $('#tbEndDate').val(app.getTDate2(data.EndDate));

            if (data.ProjectImagesJson && data.ProjectImagesJson.length > 0) {
                showFileItem(data.ProjectImagesJson, 'tbImagesList', 1);
            }
            if (data.ProjectFilesJson && data.ProjectFilesJson.length > 0) {
                showFileItem(data.ProjectFilesJson, 'tbFilesList', 2);
            }

            form.render();
        }, function (err) {
            layer.msg('请求失败');
        });
    }

    //保存
    form.on('submit(save)', function (data) {
        var fieldData = data.field;
        fieldData["GUID"] = app.getRequest('GUID');
        fieldData["ActionType"] = "Project";

        var projectImagesJson = getFileJson($('#tbImagesList'));
        var projectFilesJson = getFileJson($('#tbFilesList'));
        fieldData["ProjectImagesJson"] = projectImagesJson;
        fieldData["ProjectFilesJson"] = projectFilesJson;

        app.set("Project.ashx", fieldData, function (data) {
            layer.msg(data.msg);
            if (data.success) {
                setTimeout(function () {
                    closePage(true);
                }, 1000);
            }
        }, function (err) {
            layer.msg('请求失败');
        });
        return false;
    });

    //选择坐标
    $('#openSetMap').on('click', function () {
        layer.open({
            type: 2,
            title: '选择坐标',
            area: ['50%', '550px;'],
            maxmin: false,
            //btn: ['确定', '取消'],
            btnAlign: 'c',
            content: '/Business/MapCoordinates/SetMapPos.aspx?MapPos=' + $("#tbCoordinate").val(),
            yes: function (index, layero) {
                layer.close(index);
            }
        });
    });
});
